package com.douqu.game.mergeManager;

import com.douqu.game.MainFrame;
import com.douqu.game.MenuManager;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.mergeManager.mergeUtils.DatabaseConfig;
import com.douqu.game.mergeManager.mergeUtils.MegerJtable;
import com.douqu.game.mergeManager.mergeUtils.MergeMain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author: miracle.
 * Description:合服
 * Date: 2018/8/8 0008 下午 3:44
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class MergeManager extends MenuManager {

    //合服管理器button
    private JButton mergeBtn,mergeClearBtn;

    private MegerJtable megerTable,targetTable;

    public MergeManager(Container container, MainFrame mainFrame) {
        super(container, mainFrame);

        initRemark("<html><font color='red'>**1.使用合服数据库合并工具之前,请先去GM工具进行合服准备**</font><br/>" +
                "<font color='white'>2.目标数据库,必须新建数据库,依次填入IP,端口,数据库名,用户名,密码<br/>" +
                "3.合并数据库,依次填入合并服务器的IP,端口,数据库名,用户名,密码<br/>"+
                "4.合并数据库不能少于2个"+
                "</font></html>",90);

        JLabel targetListTitle = new JLabel();
        targetListTitle.setBounds(20,110,120,30);
        targetListTitle.setFont(MainFrame.font18);
        targetListTitle.setForeground(new Color(0, 0, 0));
        targetListTitle.setText("<html><font color='white'>" +
                "目标服务器:" +
                "</font></html>");
        container.add(targetListTitle);

//        MegerColumn targetColumn = new MegerColumn();
//        targetColumn.init(1,180,container,this);

        targetTable = new MegerJtable(1,130,container,mainFrame);

        JLabel megerListTitle = new JLabel();
        megerListTitle.setBounds(20,220,120,30);
        megerListTitle.setFont(MainFrame.font18);
        megerListTitle.setForeground(new Color(0, 0, 0));
        megerListTitle.setText("<html><font color='white'>" +
                "合并服务器:" +
                "</font></html>");
        container.add(megerListTitle);

        megerTable = new MegerJtable(5,240,container,mainFrame);

        contentText = new JTextArea();
        contentText.setFont(new Font("宋体", 0, 16));
        contentText.setEditable(false);
        contentText.setForeground(Color.WHITE);
        contentText.setBorder(null);
        contentText.setBackground(new Color(19,71,100));

        JScrollPane infoJSP = new JScrollPane(contentText);
        infoJSP.setBounds(20, 450, 600, 320);
        container.add(infoJSP);

        mergeBtn = new JButton("一键合服");
        mergeBtn.setBounds(100,780,140,30);
        mergeBtn.setFont(MainFrame.font18);
        container.add(mergeBtn);

        mergeClearBtn = new JButton("清除日志");
        mergeClearBtn.setBounds(400,780,140,30);
        mergeClearBtn.setFont(MainFrame.font18);
        container.add(mergeClearBtn);

        mergeBtn.addActionListener((ActionEvent event) -> {
            actionListener(event);
        });

        mergeClearBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });

        initEnd("合服工具");
    }

    private void actionListener(ActionEvent event){
        startActionListener();
        Object source = event.getSource();

        String result = "";
        if(source == mergeBtn){
            println("开始启动合服...");
            TwoTuple<Boolean,String> twoTuple = targetTable.initData(this);
            if (twoTuple.getFirst() == false){
                JOptionPane.showMessageDialog(mainFrame, twoTuple.getSecond());
            }else{
                println("检测目标服务器完成");
                twoTuple = megerTable.initData(this);
                if (twoTuple.getFirst() == false){
                    JOptionPane.showMessageDialog(mainFrame, twoTuple.getSecond());
                }else{
                    println("检测合并服务器完成");
                    startMeger();
                }
            }
        }else if (source == mergeClearBtn){
            contentText.setText("");
        }
        endActionListener();
    }

    /**
     * 一键合并
     */
    private void startMeger(){
        println("开始合并....");
        if (megerTable.getDatabaseConfigs().contains(targetTable.getDatabaseConfigs().get(0))){
            JOptionPane.showMessageDialog(mainFrame, "合并服务器不可作为目标服务器");
            println("合并服务器不可作为目标服务器");
            return;
        }else{
            for (int i = 0; i < megerTable.getDatabaseConfigs().size(); i ++){
                for (int j = 0 ; j < megerTable.getDatabaseConfigs().size(); j ++){
                    if (j == i){
                        continue;
                    }
                    if (megerTable.getDatabaseConfigs().get(i).equals(megerTable.getDatabaseConfigs().get(j))){
                        JOptionPane.showMessageDialog(mainFrame, "合并服务器第" + i + "与第" + j + "行重复！");
                        println("合并服务器第" + i + "与第" + j + "行重复！");
                        working = false;
                        return;
                    }
                }
            }

            MergeMain main = new MergeMain();
            try{
                DatabaseConfig targetConfig = targetTable.getDatabaseConfigs().get(0);
                DatabaseConfig[] megerConfigArray = new DatabaseConfig[megerTable.getDatabaseConfigs().size()];
                for (int i = 0; i < megerTable.getDatabaseConfigs().size(); i ++){
                    megerConfigArray[i] = megerTable.getDatabaseConfigs().get(i);
                }
                if (main.merge(this,targetConfig ,megerConfigArray)){
                    println("合并完成");
                }
            }catch (Exception e){
                e.printStackTrace();
                println(e.getMessage());
                println("合并失败");
                working = false;
            }
        }
    }
}
